﻿using System;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.DAL;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
            this.CenterToScreen();
            HideDBSettingPanel();
        }
        bool isShowSettingPanel = false;
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            try
            {
                if (!AppCore.IsInit())
                {
                    lblSet.Text = "[提示] 初次使用,请先配置数据库";
                    DisableLoginPanel();
                    ShowDBSettingPanel();
                    return;
                }
                EnableLoginPanel();
                var info = AppCore.GetConnectionInfo();
                DBHelper.Conn.ConnectionString = info.ConnectionString;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ShowDBSettingPanel()
        {
            isShowSettingPanel = true;
            this.Width = 464;
            this.Height = 740;
            btnDbSet.Text = "关闭";
        }

        /// <summary>
        /// 
        /// </summary>
        private void HideDBSettingPanel()
        {
            isShowSettingPanel = false;
            this.Width = 464;
            this.Height = 324;
            btnDbSet.Text = "配置";
        }

        private void EnableLoginPanel()
        {
            txtUserName.Enabled = true;
            txtUserName.Focus();

            txtUserPassword.Enabled = true;
            btnSaleLogin.Enabled = true;
            btnManageLogin.Enabled = true;
            btnAddUser.Enabled = true;
            llblForgetPassword.Enabled = true;

            lblSet.Text = "[配置]按钮用于配置数据库";
        }

        /// <summary>
        /// 
        /// </summary>
        private void DisableLoginPanel()
        {
            txtUserName.Enabled = false;
            txtUserPassword.Enabled = false;
            btnSaleLogin.Enabled = false;
            btnManageLogin.Enabled = false;
            btnAddUser.Enabled = false;
            llblForgetPassword.Enabled = false;
        }

        private void btnDbSet_Click(object sender, EventArgs e)
        {

            if (isShowSettingPanel)
            {
                HideDBSettingPanel();
            }
            else
            {
                ShowDBSettingPanel();
            }
        }

        private void btnOkWin_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtServerWin.Text.Trim()))
                {
                    MessageBox.Show("请输入数据库服务器名称!", "提示");
                    txtServerWin.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(txtDataBaseWin.Text.Trim()))
                {
                    MessageBox.Show("请输入数据库名称!", "提示");
                    txtDataBaseWin.Focus();
                    return;
                }

                string serverName = txtServerWin.Text.Trim();
                string dataBaseName = txtDataBaseWin.Text.Trim();
                AppCore.SetConnectionInfo(new ConnectionInfo()
                {
                    AuthType = AuthType.WinAuth,
                    ConnectionString = AppCore.GetWinConnString(serverName, dataBaseName)
                });
                var info = AppCore.GetConnectionInfo();
                DBHelper.Conn.ConnectionString = info.ConnectionString;
                EnableLoginPanel();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnOkSql_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtServerSql.Text.Trim()))
                {
                    MessageBox.Show("请输入数据库服务器名称!", "提示");
                    txtServerSql.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(txtDataBaseSql.Text.Trim()))
                {
                    MessageBox.Show("请输入数据库名称!", "提示");
                    txtDataBaseSql.Focus();
                    return;
                }

                if (string.IsNullOrEmpty(txtUserSql.Text.Trim()))
                {
                    MessageBox.Show("请输入用户名!", "提示");
                    txtUserSql.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(txtPasswordSql.Text.Trim()))
                {
                    MessageBox.Show("请输入密码!", "提示");
                    txtPasswordSql.Focus();
                    return;
                }

                string serverName = txtServerSql.Text.Trim();
                string dataBaseName = txtDataBaseSql.Text.Trim();
                string uid = txtUserSql.Text.Trim();
                string pwd = txtPasswordSql.Text.Trim();
                AppCore.SetConnectionInfo(new ConnectionInfo()
                {
                    AuthType = AuthType.SqlServerAuth,
                    ConnectionString = AppCore.GetSqlConnString(serverName, dataBaseName, uid, pwd)
                });
                var info = AppCore.GetConnectionInfo();
                DBHelper.Conn.ConnectionString = info.ConnectionString;
                EnableLoginPanel();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSaleLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string userName = txtUserName.Text.Trim();
                string userPassword = txtUserPassword.Text.Trim();
                string regexUser = @"^[a-zA-Z]\w{4,31}$";
                if (!Regex.IsMatch(userName, regexUser))
                {
                    MessageBox.Show("用户名格式不正确，请重新输入", "提示");
                    txtUserName.Text = "";
                    txtUserPassword.Text = "";
                    txtUserName.Focus();
                    return;
                }
                if (userPassword.Length < 6 || userPassword.Length > 32)
                {
                    MessageBox.Show("密码为6-32个字符，请重新输入", "提示");
                    txtUserPassword.Text = "";
                    txtUserPassword.Focus();
                    return;
                }
                UserManager userManager = new UserManager();
                string result = userManager.UserLogin(userName, userPassword);
                if (result == "OK")
                {
                    UserManager.loginType = "SaleLogin";
                    FrmGoodsSale ob_FrmGoodsSale = new FrmGoodsSale();
                    ob_FrmGoodsSale.Show();
                    this.Hide();
                }
                else if (result == "UserName Error")
                {
                    MessageBox.Show("用户名错误，请重新输入", "提示");
                    txtUserName.Text = "";
                    txtUserPassword.Text = "";
                    txtUserName.Focus();
                }
                else
                {
                    MessageBox.Show("密码错误，请重新输入", "提示");
                    txtUserPassword.Text = "";
                    txtUserPassword.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnManageLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string userName = txtUserName.Text.Trim();
                string userPassword = txtUserPassword.Text.Trim();
                string regexUser = @"^[a-zA-Z]\w{4,31}$";
                if (!Regex.IsMatch(userName, regexUser))
                {
                    MessageBox.Show("用户名格式不正确，请重新输入", "提示");
                    txtUserName.Text = "";
                    txtUserPassword.Text = "";
                    txtUserName.Focus();
                    return;
                }
                if (userPassword.Length < 6 || userPassword.Length > 32)
                {
                    MessageBox.Show("密码为6-32个字符，请重新输入", "提示");
                    txtUserPassword.Text = "";
                    txtUserPassword.Focus();
                    return;
                }
                UserManager userManager = new UserManager();
                string result = userManager.UserLogin(userName, userPassword);
                if (result == "OK")
                {
                    UserManager.loginType = "ManageLogin";
                    FrmMain ob_FrmMain = new FrmMain();
                    ob_FrmMain.Show();
                    this.Hide();
                }
                else if (result == "UserName Error")
                {
                    MessageBox.Show("用户名错误，请重新输入", "提示");
                    txtUserName.Text = "";
                    txtUserPassword.Text = "";
                    txtUserName.Focus();
                }
                else
                {
                    MessageBox.Show("密码错误，请重新输入", "提示");
                    txtUserPassword.Text = "";
                    txtUserPassword.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtUserPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (txtUserName.Text.Trim() == "Admin" || txtUserName.Text.Trim() == "admin")
                {
                    btnManageLogin_Click(sender, e);
                }
                else
                {
                    btnSaleLogin_Click(sender, e);
                }
            }
        }

        private void btnClose_Click_1(object sender, EventArgs e)
        {
            Environment.Exit(1);
        }

        private void btnAddUser_Click_1(object sender, EventArgs e)
        {
            AppCore.FrmAddUser.Show();
            AppCore.FrmAddUser.Activate();
        }

        private void llblForgetPassword_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
        {
            AppCore.FrmGetPassword.Show();
            AppCore.FrmGetPassword.Activate();
        }

        private void btnSaleLogin_Click_1(object sender, EventArgs e)
        {
            AppCore.FrmGoodsSale.Show();
            AppCore.FrmGoodsSale.Activate();
            this.Hide();
        }

        private void btnManageLogin_Click_1(object sender, EventArgs e)
        {
            AppCore.FrmMain.Show();
            AppCore.FrmMain.Activate();
            this.Hide();
        }
    }

}
